# -*- coding: utf-8 -*-
"""
检查Agent发布状态
"""

import os
import sys
from pathlib import Path
from dotenv import load_dotenv

# 添加项目根目录到路径
project_root = Path(__file__).parent.parent
sys.path.insert(0, str(project_root))

# 加载环境变量
load_dotenv(project_root / '.env')

from scripts.auto_publish_agent import AgentAutoPublisher


def main():
    """检查发布状态"""

    # 已存在的AppID
    app_id = "d3rincoiqq79p3439na0"

    print("=" * 80)
    print(f"检查Agent发布状态: {app_id}")
    print("=" * 80)

    publisher = AgentAutoPublisher()
    publisher.app_id = app_id

    try:
        # 检查发布历史
        print("\n[检查] 查询发布历史...")
        result = publisher.check_publish_status()

        items = result.get("Items", [])
        if result.get("Response"):
            items = result["Response"].get("Items", [])

        if items:
            print(f"\n找到 {len(items)} 条发布记录:\n")
            for idx, item in enumerate(items, 1):
                print(f"记录 {idx}:")
                print(f"  PublishID: {item.get('PublishID')}")
                print(f"  版本: {item.get('Version')}")
                print(f"  状态: {item.get('Status')}")
                print(f"  创建时间: {item.get('CreatedAt')}")

                # 智能体中心状态
                if "AppCenterStatus" in item:
                    center = item["AppCenterStatus"]
                    print(f"  智能体中心:")
                    print(f"    状态: {center.get('Status')}")
                    print(f"    消息: {center.get('Message', 'N/A')}")
                print()
        else:
            print("\n未找到发布记录")

        # 获取App信息
        print("\n[检查] 获取App详细信息...")
        app_result = publisher.get_published_app_info()

        print("\n" + "=" * 80)
        print("当前状态:")
        print("=" * 80)

    except Exception as e:
        print(f"\n[错误] 检查失败: {str(e)}")
        import traceback
        traceback.print_exc()


if __name__ == "__main__":
    main()
